package clientapp;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class AddMovieFrame extends JFrame implements ActionListener
{
	static int DvdIDCount = 1030;
	
	private JButton btnAdd = new JButton("Add");
	private JButton btnCancel = new JButton("Cancel");
	private JRadioButton rbNew, rbOld;
	private JTextField txtTitle = new JTextField(8);
	private JComboBox cmbCategory;//edit this
	private String categoryItems[] = {"Action","Adventure","Animated","Romance","Comedy",
		"Drama","Fantasy","Horror","Mystery","Sci-Fi","Thriller","War",};
	private Client clientr;
	public AddMovieFrame(Client clientp)
	{
		super("Add Movie");
        clientr =(Client)clientp;
		setSize(300,210);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		addWindowListener (new WindowAdapter ()
		{
	    	public void windowClosing (WindowEvent e)
	    	{ //overiding windowClosing
				runStore.store.setEnabled(true);
	    	}
		} //the improved WindowListener is added.
		);
		
		setLayout(new BorderLayout());
		//setResizable(false);
		JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		JPanel topPanel = new JPanel(new GridLayout(2,2));//top panel
		topPanel.add(new JLabel("Title:"));
		topPanel.add(txtTitle);
		topPanel.add(new JLabel("Category:"));
		cmbCategory = new JComboBox(categoryItems);//edit
		topPanel.add(cmbCategory);
		
		JPanel rbPanel = new JPanel(new GridLayout(3,1));//radio button panel
		rbPanel.add(new JLabel("Select DVD release type:"));
		rbNew = new JRadioButton("New Release",true);
		rbOld = new JRadioButton("Old Release");
		ButtonGroup bg = new ButtonGroup();
		bg.add(rbNew);
		bg.add(rbOld);
		rbPanel.add(rbNew);
		rbPanel.add(rbOld);
		
		//center panel
		centerPanel.add(topPanel);
		centerPanel.add(rbPanel);
		
		//south panel
		southPanel.add(btnAdd);
		southPanel.add(btnCancel);
		
		
		//add listener to button
		btnCancel.addActionListener(this);
		btnAdd.addActionListener(this);
		
		add(new JPanel(),BorderLayout.WEST);
		add(centerPanel,BorderLayout.CENTER);
		add(southPanel,BorderLayout.SOUTH);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()== btnCancel)
		{
			//runStore.store.setEnabled(true);
			dispose();
		}
		else
			if(e.getSource()==btnAdd)
			{
					String title = txtTitle.getText();
					 
					if(title.trim().equals(""))
						JOptionPane.showMessageDialog(null,"Title cannot be null or contain only spaces");
					else
					{
						boolean newRelease = rbNew.isSelected();
						String category = (String)cmbCategory.getSelectedItem();
						
						String sendToServer = "AddMovie" + "#" + title + "#" + category + "#" + newRelease;
						clientr.sendQuery(sendToServer);
						JOptionPane.showMessageDialog(null, "The Movie was successfully added");
						
						dispose();
					}
			}
	}
}